/**
 * ClassName:AdminKeyWordController
 * Package:com.cskaoyan.market.controller
 * Description:
 *
 * @Author LY
 * @Create 2024/6/19 20:32
 * @Version1.0
 **/

package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketKeyword;
import com.cskaoyan.market.service.AdminKeyService;
import com.cskaoyan.market.service.impl.AdminKeyServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

@WebServlet("/admin/keyword/*")
public class AdminKeyWordController extends HttpServlet {
    AdminKeyService adminKeyService=new AdminKeyServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/keyword/", "");
        if("list".equals(op))
        {
            list(req,resp);
        }
    }
    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String keyword = req.getParameter("keyword");
        String url = req.getParameter("url");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");

        List<MarketKeyword> keywordList=adminKeyService.list(page,limit,keyword,url,sort,order);
        Object o = ResponseUtil.okList(keywordList);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(o);
        resp.getWriter().println(s);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/keyword/", "");
        if("create".equals(op))
        {
            create(req,resp);
        }
        else if ("delete".equals(op)) {
            delete(req,resp);
        }
        else if("update".equals(op))
        {
            update(req,resp);
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String reqBody=req.getReader().readLine();
        MarketKeyword marketKeyword = JacksonUtil.getObjectMapper().readValue(reqBody, MarketKeyword.class);
        if(adminKeyService.update(marketKeyword))
        {
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok(marketKeyword)));
        }
        else
        {
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.fail(810,"更新失败")));
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        String marketKeyWordJson = req.getReader().readLine();
        MarketKeyword marketKeyword = objectMapper.readValue(marketKeyWordJson, MarketKeyword.class);
        if (adminKeyService.delete(marketKeyword)==1)
        {
            Object ok = ResponseUtil.ok();
            String s = JacksonUtil.writeValueAsString(ok);
            resp.getWriter().write(s);
            return;
        }
        //todo 删除失败的情况
    }

    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        String s = req.getReader().readLine();
        MarketKeyword marketKeyword = objectMapper.readValue(s, MarketKeyword.class);

        marketKeyword.setAddTime(LocalDateTime.now());
        marketKeyword.setUpdateTime(LocalDateTime.now());
        if(adminKeyService.create(marketKeyword)==1)
        {
            Object ok = ResponseUtil.ok(marketKeyword);
            String respJson = JacksonUtil.getObjectMapper().writeValueAsString(ok);
            resp.getWriter().write(respJson);
        }
    }
}
